home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.09 Sep 88 / Forth Stuff / AppleTalk_mail next >
Encoding:
Text File  |  1988-09-06  |  12.4 KB  |  598 lines  |  [TEXT/EDIT]

  1. only forth also assembler
  2.  
  3. \ Appletalk general definitions
  4. \ 30.05.88 JL
  5.  
  6. vocabulary network
  7. also network definitions
  8.  
  9. DECIMAL
  10.  
  11. 27    constant    ioPermission
  12. 18    constant    ioFileName
  13. 18    constant    userData
  14. 24    constant    ioRefNum
  15. 26    constant    csCode
  16. 28    constant    socket
  17. 30    constant    addrBlock
  18.  
  19. 4    constant    atpLoadedBit
  20. 1    constant    useATalk
  21. -97    constant    portInUse
  22. -98    constant    portNotCf
  23.  
  24. 9    constant     mppUnitNum    
  25. 10    constant     atpUnitNum    
  26. mppUnitNum 1+ negate    
  27.     constant     mppRefNum
  28. atpUnitNum 1+ negate
  29.     constant     atpRefNum
  30.  
  31. \ LAP defs
  32. 1    constant    LAPshortDDP
  33. 2    constant    LAPLongDDP
  34. -94    constant    lapProtErr
  35. -95    constant    lapExcessCollns
  36.  
  37. 243    constant    lapWrite
  38. 244    constant    lapDetachPH
  39. 245    constant    lapAttachPH
  40.  
  41. -1    constant    lapOverrunErr
  42. -2    constant    lapCRCErr
  43. -3    constant    lapUnderrunErr
  44. -4    constant    lapLengthErr
  45.  
  46. \ DDP defs
  47. 5    constant    ddpHdSzShort
  48. 13    constant    ddpHdSzLong
  49.  
  50. 1    constant    ddpRTMP
  51. 2    constant    ddpNBP
  52. 3    constant    ddpATP
  53.  
  54. $7F    constant    ddpMaxWKS
  55. 586    constant    ddpMaxData
  56. $3ff    constant    ddpLengthMask
  57. 128    constant    ddpWKS
  58.  
  59. -91    constant    ddpSktErr
  60. -92    constant    ddpLenErr
  61. -93    constant    ddpNoBridgeErr
  62.  
  63. \ CsCode values for DDP Control calls- MPP
  64. 246    constant    ddpWrite
  65. 247    constant    ddpCloseSkt
  66. 248    constant    ddpOpenSkt
  67.  
  68. \ RTMP definitions
  69. 1    constant    rtmpSkt
  70.  
  71. \ NBP definitions
  72. $10    constant    nbpBrRq
  73. $20    constant    nbpLkUp
  74. $30    constant    nbpLkUpReply
  75. 2    constant    nbpSkt
  76. 15    constant    nbpTupleMax
  77. ascii = constant    nbpEquals
  78. ascii * constant    nbpStar
  79.  
  80. 0    constant    ntLink
  81. 4    constant    ntTuple
  82. 7    constant    ntSocket
  83. 9    constant    ntEntity
  84.  
  85. -1024    constant    nbpBuffOvr
  86. -1025    constant    nbpNoConfirm
  87. -1026    constant    nbpConfDiff
  88. -1027    constant    nbpDuplicate
  89. -1028    constant    nbpNotFound
  90. -1029    constant    nbpNISErr
  91.  
  92. 249    constant    nbpLoad
  93. 250    constant    nbpConfirm
  94. 251    constant    nbpLookup
  95. 252    constant    nbpRemove
  96. 253    constant    nbpRegister
  97. 254    constant    nbpKill
  98. 255    constant    nbpUnload
  99. 256    constant     setSelfSend
  100.  
  101. \ ATP definitions
  102. $40    constant    atpReqCode      
  103. $80    constant    atpRspCode
  104. $C0    constant    atpRelCode
  105. $20    constant    atpXO
  106. $10    constant    atpEOM
  107. $08    constant    atpSTS
  108. $02    constant    atpTidValid
  109. $01    constant    atpSendChk
  110. $3F    constant    atpFlagMask
  111. $F8    constant    atpControlMask
  112.  
  113. 8    constant    atpMaxNum
  114. 578    constant    atpMaxData
  115.  
  116. 249    constant    atpRelRspCB
  117. 250    constant    atpCloseSkt
  118. 251    constant    atpAddResponse
  119. 252    constant    atpSendResponse
  120. 253    constant    atpGetRequest
  121. 254    constant    atpOpenSkt
  122. 255    constant    atpSendRequest
  123. 256    constant    atpRelTCB
  124.  
  125. -1096    constant    atpReqFailed
  126. -1097    constant    atpTooManyReqs
  127. -1098    constant    atpTooManySkts
  128. -1099    constant    atpBadATPSkt
  129. -1100    constant    atpBadBuffNum
  130. -1101    constant    atpNoRelErr
  131. -1102    constant    atpCBNotFound
  132. -1103    constant    atpNoSendResp
  133. -1104    constant    atpNoDataArea
  134. -1105    constant    atpReqAborted
  135.  
  136. $1FA    constant    pRamByte
  137. $1FB    constant    SPConfig
  138. $291    constant    portBUse
  139. $2D8    constant    ABusVars
  140. $2DC    constant    ABusDCE
  141.  
  142. 0 constant bdsBuffSz
  143. 2 constant bdsBuffAddr
  144. 6 constant bdsDataSz
  145. 8 constant bdsUserData 
  146.  
  147. .trap     _control,async    $a404
  148.  
  149. header ATPbuffer 2000 allot
  150. header myBDS 8 12 * allot
  151. header reqBuf 600 allot
  152. header myNTE 100 allot
  153. header ATPblock 50 allot
  154.  
  155. header MPPName
  156.     DC.B    4
  157.     DC.B    '.MPP'
  158.  
  159. header ATPName
  160.     DC.B    4
  161.     DC.B    '.ATP'
  162.  
  163. ( Have to make sure that AppleTalk can be run. See if port is occupied)
  164. ( pg. 55 from Inside Mac's AppleTalk Guide)
  165.  
  166. : open.atp
  167.     PortBUse c@ $10 and
  168.     IF 0 \ Appletalk already open!
  169.     ELSE
  170.         ['] ATPName ['] ATPBlock ioFileName + !
  171.         0 ['] ATPBlock ioPermission + c!
  172.         ['] ATPBlock call Open  
  173.     THEN
  174. ;
  175.  
  176. : open.mpp
  177.         ['] MPPName ['] ATPBlock ioFileName + !
  178.         0 ['] ATPBlock ioPermission + c!
  179.         ['] ATPBlock call Open  
  180. ;    
  181.  
  182. : OpenATalk { | PBUse -- f }
  183.     PortBUse c@ -> PBUse
  184.  
  185.     PBUse 0<
  186.     IF SPConfig c@ $F AND useATalk = 
  187.         IF open.mpp ?dup 0=
  188.             IF open.atp THEN
  189.         ELSE
  190.             portNotCf
  191.         THEN
  192.     ELSE 
  193.         PBUse $F and useATalk = 
  194.         IF open.atp 
  195.         ELSE portInUse
  196.         THEN
  197.     THEN
  198. ;
  199.  
  200. : close.atp
  201.     ATPRefNum ['] ATPBlock ioRefNum + w!
  202.     ['] ATPBlock call Close
  203. ;
  204.  
  205. : call.mpp
  206.     mppRefNum     ['] ATPBlock ioRefNum + w!
  207.     ['] ATPBlock call control
  208. ;
  209.     
  210. : call.atp
  211.     atpRefNum     ['] ATPBlock ioRefNum + w!
  212.     ['] ATPBlock call control
  213. ;
  214.  
  215. : call.atp.async ( p_complete -- flag )
  216.     atpRefNum         ['] ATPBlock ioRefNum + w!
  217.     ( p_complete)     ['] ATPBlock ioCompletion + !
  218.     LEA        ATPBlock,A0
  219.     EXG        D4,A7
  220.     _Control,Async
  221.     EXT.L    D0
  222.     MOVE.L    D0,-(A6)
  223.     EXG        D4,A7
  224. ;
  225.     
  226. : open.socket    ( addrBlock socket# -- socket# flag )
  227.     ( socket# )     ['] ATPBlock socket + c!
  228.     ( addrBlock )    ['] ATPBlock addrBlock + !
  229.     atpOpenSkt        ['] ATPBlock csCode + w!
  230.     call.atp
  231.     ['] ATPBlock socket + c@
  232.     swap
  233. ;
  234.  
  235. : close.socket ( socket# -- flag )
  236.     ( socket# )     ['] ATPBlock socket + c!
  237.     atpCloseSkt     ['] ATPBlock csCode + w!
  238.     call.atp
  239. ;
  240.  
  241. : (send.request)  ( userData atpFlags addrBlock
  242.                     reqLength reqPointer 
  243.                     bdsPointer numOfBuffs
  244.                     timeOutVal retryCount -- 
  245.                     reqTID BitMap atpFlags numOfResps flag )
  246.     ( retryCount )    ['] ATPBlock 47 + c!
  247.     ( timeOutVal )    ['] ATPBlock 45 + c!
  248.     ( numOfBuffs )    ['] ATPBlock 44 + c!
  249.     ( bdsPointer )    ['] ATPBlock 40 + !
  250.     ( reqPointer )  ['] ATPBlock 36 + !
  251.     ( reqLength )    ['] ATPBlock 34 + w!
  252.     ( addrBlock )     ['] ATPBlock 30 + !
  253.     ( atpFlags )    ['] ATPBlock 29 + c!
  254.     ( userData )    ['] ATPBlock 18 + !
  255.     atpSendRequest    ['] ATPBlock csCode + w!
  256.     call.atp
  257.     ['] ATPBlock 16 + w@
  258.     ['] ATPBlock 28 + c@
  259.     ['] ATPBlock 29 + c@
  260.     ['] ATPBlock 46 + c@
  261.     4 roll ( result code )
  262. ;
  263.  
  264. : (get.request)      ( atpSocket reqLength reqPointer --
  265.                     userData atpFlags addrBlock reqLength
  266.                     bitMap transID flag )
  267.     ( reqPointer )    ['] ATPBlock 36 + !
  268.     ( reqLength )    ['] ATPBlock 34 + w!
  269.     ( atpSocket )    ['] ATPBlock 28 + c!
  270.     call.atp
  271.     ['] ATPBlock 18 + @
  272.     ['] ATPBlock 29 + c@
  273.     ['] ATPBlock 30 + @
  274.     ['] ATPBlock 34 + w@
  275.     ['] ATPBlock 44 + c@
  276.     ['] ATPBlock 46 + c@
  277.     6 roll ( result code )
  278. ;
  279.  
  280. variable ATPout ( semaphore )
  281.  
  282. code get.request.compl
  283.     movem.l    a0/a5,-(a7)
  284.     movea.l    currenta5,a5
  285.     movea.l    ATPout,a0
  286.     move.w    #wake,(a0)
  287.     movem.l    (a7)+,a0/a5
  288.     rts
  289. end-code
  290.     
  291. : (get.request.async)      ( atpSocket reqLength reqPointer --
  292.                     userData atpFlags addrBlock reqLength
  293.                     bitMap transID flag )
  294.     ATPout get
  295.     ( reqPointer )    ['] ATPBlock 36 + !
  296.     ( reqLength )    ['] ATPBlock 34 + w!
  297.     ( atpSocket )    ['] ATPBlock 28 + c!
  298.     ['] get.request.compl call.atp.async
  299.     sleep status w! pause \ wake up when getRequest is completed
  300.     ['] ATPBlock 18 + @
  301.     ['] ATPBlock 29 + c@
  302.     ['] ATPBlock 30 + @
  303.     ['] ATPBlock 34 + w@
  304.     ['] ATPBlock 44 + c@
  305.     ['] ATPBlock 46 + c@
  306.     6 roll ( result code )
  307.     ATPout release
  308. ;
  309.  
  310. : setup.bds ( #buffers )
  311.     0 DO
  312.         600 call NewPtr abort" Could not get buffer memory"
  313.         i 12 * ['] myBDS + 2+ !
  314.     LOOP
  315. ;
  316.  
  317. : release.bds ( #buffers )
  318.     0 DO
  319.         i 12 * ['] myBDS + 2+ @ 
  320.         call DisposPtr abort" DisposPtr failed!"
  321.     LOOP
  322. ;
  323.     
  324. : (send.response) ( atpSocket atpFlags addrBlock
  325.                     bdsPointer numOfBuffs bdsSize transID --
  326.                     reqTID userData flag )
  327.     ( transID )        ['] ATPBlock 46 + w!
  328.     ( bdsSize )        ['] ATPBlock 45 + c!
  329.     ( numOfBuffs )    ['] ATPBlock 44 + c!
  330.     ( bdsPointer )    ['] ATPBlock 40 + !
  331.     ( addrBlock )     ['] ATPBlock 30 + !
  332.     ( atpFlags )    ['] ATPBlock 29 + c!
  333.     ( atpSocket )    ['] ATPBlock 28 + c!
  334.     atpSendResponse    ['] ATPBlock csCode + w!
  335.     call.atp
  336.     ['] ATPBlock 16 + w@
  337.     ['] ATPBlock 18 + @
  338.     rot ( result code )
  339. ;
  340.  
  341. : (add.response)  ( userData atpSocket atpFlags addrBlock
  342.                     reqLength reqPointer rspNum transID --
  343.                     flag )
  344.     ( transID )        ['] ATPBlock 46 + w!
  345.     ( rspNum )        ['] ATPBlock 44 + c!
  346.     ( reqPointer )    ['] ATPBlock 36 + !
  347.     ( reqLength )    ['] ATPBlock 34 + w!
  348.     ( addrBlock )     ['] ATPBlock 30 + !
  349.     ( atpFlags )    ['] ATPBlock 29 + c!
  350.     ( atpSocket )    ['] ATPBlock 28 + c!
  351.     ( userData )    ['] ATPBlock 18 + !
  352.     atpAddResponse    ['] ATPBlock csCode + w!
  353.     call.atp ( result code )
  354. ;
  355.  
  356. : load.nbp
  357.     nbpLoad ['] ATPBlock csCode + w!
  358.     call.mpp
  359. ;
  360.  
  361. : make.entity { object typ zone entity | obL typL -- }
  362.     object entity         over c@ 1+ dup -> obL    cmove
  363.     typ    entity obL +    over c@ 1+ dup -> typL    cmove
  364.     zone   entity obL + typL +  over c@ 1+         cmove
  365. ;
  366.  
  367. : (lookup.name) ( interval retry buffer size max entity -- 
  368.                  matches flag )
  369.     nbpLookup     ['] ATPBlock csCode + w!
  370.     ( entity )     ['] ATPBlock 30 + !
  371.     ( max )        ['] ATPBlock 40 + w!
  372.     ( size )    ['] ATPBlock 38 + w!
  373.     ( buffer )    ['] ATPBlock 34 + !
  374.     ( retry )    ['] ATPBlock 29 + c!
  375.     ( interval) ['] ATPBlock 28 + c!
  376.     call.mpp
  377.     ['] ATPBlock 42 + w@ \ matches found
  378.     swap \ result code
  379. ;
  380.  
  381. : lookup.name ( object typ zone | -- matches flag )
  382.     ['] myNTE ntEntity + make.entity
  383.     2 10 ['] ATPbuffer 600 20 ['] myNTE ntEntity +    
  384.         (lookup.name)
  385. ;
  386.  
  387. : (register.name) ( interval retry ntQEl verify --  flag )
  388.     nbpRegister     ['] ATPBlock csCode + w!
  389.     ( verify )    ['] ATPBlock 34 + c!
  390.     ( ntQEl )     ['] ATPBlock 30 + !
  391.     ( retry )    ['] ATPBlock 29 + c!
  392.     ( interval)     ['] ATPBlock 28 + c!
  393.     call.mpp    \ result code
  394. ;
  395.  
  396. : register.name ( socket# object typ zone ) { ntQEl | -- flag }
  397.     ntQEl ntEntity + make.Entity
  398.     ntQEl ntSocket + c! ( store socket number )
  399.     2 10 ntQEl 1 ( always verify ) (register.name)
  400. ;
  401.  
  402. : remove.name ( ntQEl | flag )
  403.     nbpRemove     ['] ATPBlock csCode + w!
  404.     ( ntQEl ) ntEntity +    ['] ATPBlock 30 + !
  405.     call.mpp    \ result code
  406. ;
  407.  
  408. : set.self.send ( self_send_flag | old_flag -- )
  409.     setSelfSend ['] ATPBlock csCode + w!
  410.     ( flag )    ['] ATPBlock 28 + c!
  411.     call.mpp    drop    \ result code
  412.     ['] ATPBlock 29 + c@
  413. ;
  414.  
  415. 4ascii STR     constant "str
  416. 4ascii MAIL constant "mail
  417.  
  418. : get.choosername
  419.     "str -16096 call getresource
  420.     ?dup IF @ ELSE 1 abort" No Chooser name found!" THEN
  421. ;
  422.         
  423. also forth definitions
  424.  
  425. variable mailbox.socket
  426. header mailNTE 110 allot
  427.  
  428. : open.mailbox
  429.     0 0 open.socket abort" could not get free ATP socket"
  430.     dup mailbox.socket !
  431.         get.choosername " mailbox" " *" ['] mailNTE
  432.         register.name abort" registerName failed"
  433. ;
  434.  
  435. : close.mailbox
  436.     ['] mailNTE remove.name drop
  437.     mailbox.socket @ close.socket drop
  438. ;
  439.  
  440. : sendOK
  441.     cr ." ----- Sending OK response....."
  442.     1 setup.bds
  443.     " This mail was received OK." count dup ['] myBDS w!
  444.                                 ['] myBDS 2+ @ swap cmove
  445.     ['] myBDS    ['] ATPBlock 40 + !
  446.     1    ['] ATPBlock 44 + c!
  447.     1    ['] ATPBlock 45 + c!
  448.     atpSendResponse    ['] ATPBlock csCode + w!
  449.     call.atp
  450.     1 release.bds
  451. ;
  452.  
  453. : get.mail { | trID addr.block -- reqTID userData flag }
  454.     mailbox.socket @ 500 ['] reqBuf (get.request)
  455.     abort" ATPGetRequest error!"
  456.     5 call sysbeep
  457.     -> trID drop ( don't need bitmap )
  458.     cr ." ***** Mail received *****"
  459.     cr ['] reqBuf swap type
  460.     cr ." ***** End of mail   *****"
  461.     dup -> addr.block
  462.     cr ." sender: $" hex . ." , flags: $" . ." , User Data: $" . 
  463.     cr decimal
  464.     sendOK
  465.  
  466. : get.mail.async { | trID addr.block -- reqTID userData flag }
  467.     mailbox.socket @ 500 ['] reqBuf (get.request.async)
  468.     abort" ATPGetRequest error!"
  469.     5 call sysbeep
  470.     -> trID drop ( don't need bitmap )
  471.     cr ." ***** Mail received *****"
  472.     cr ['] reqBuf swap type
  473.     cr ." ***** End of mail   *****"
  474.     dup -> addr.block
  475.     cr ." sender: $" hex . ." , flags: $" . ." , User Data: $" . 
  476.     cr decimal
  477.     sendOK
  478.  
  479. : >@< ( odd address fetch, unnecessary on MacII )
  480.     dup 2 mod
  481.     IF dup c@ swap 1+ @ -8 scale $FFFFFF and swap 24 scale +
  482.     ELSE @
  483.     THEN
  484. ;
  485.      
  486. : next.field dup c@ + 1+ ;
  487.  
  488. : print.entities ( #entities entityTable )
  489.     cr swap hex
  490.     0 DO ." $" dup >@< u. ."  - "
  491.          5 + dup count type ." :" 
  492.          next.field dup count type ." @" 
  493.          next.field dup count type cr
  494.          next.field
  495.     LOOP drop
  496.     decimal
  497. ;
  498.  
  499. : find.boxes
  500.     " =" " mailbox" " *" lookup.name
  501.         abort" LookupName failed"
  502.     cr ?dup IF dup . ." mailbox(es) found on the network:" cr
  503.                     ['] ATPBuffer print.entities
  504.             ELSE ." No mailboxes found on the network." cr
  505.             THEN
  506.         
  507. : send.mail { receiver msg | -- }
  508.     cr ." Sending message to $" 
  509.         receiver hex . decimal ." ..." cr
  510.     1 setup.bds
  511.     "mail %00100000 receiver
  512.     msg count swap ['] myBDS 1 2 5 
  513.     (send.request)
  514.     ?dup IF ." SendRequest error #" . cr
  515.         ELSE ." Mail delivered" cr 
  516.         THEN
  517.     . . . . cr
  518.     1 release.bds
  519. ;
  520.  
  521. \ ===== DEFINE MAIL SENDER AND RECEIVER TASKS =====
  522.  
  523. also mac
  524.  
  525. NEW.WINDOW Sender
  526. " Sender" Sender TITLE
  527. 40 20 170 400 Sender BOUNDS
  528. Document Visible CloseBox GrowBox Sender ITEMS
  529.  
  530. 400 1000 TERMINAL sendTask
  531.  
  532. NEW.WINDOW Receiver
  533. " Mailbox" Receiver TITLE
  534. 190 20 320 400 Receiver BOUNDS
  535. Document Visible CloseBox GrowBox Receiver ITEMS
  536.  
  537. 400 1000 TERMINAL rcvTask
  538.  
  539. : mail.it
  540.     activate
  541.     begin
  542.         cr ." Searching open mailboxes..." cr
  543.         find.boxes
  544.         hex
  545.         begin
  546.             cr ." To address (zero to quit): $" 
  547.                 pad dup 1+ 10 expect number? 
  548.         until
  549.  
  550.         ?dup IF
  551.             cr ." Message: " 
  552.             pad 1+ 80 expect 
  553.             span pad c!
  554.             pad send.mail
  555.         ELSE bye 
  556.         THEN
  557.     again
  558. ;
  559.  
  560. : get.it
  561.     activate
  562.     begin
  563.     cr ." Registering new Mailbox..."
  564.     open.mailbox
  565.         cr ." Waiting for mail..." cr
  566.         get.mail.async drop
  567.     close.mailbox
  568.     ?terminal until
  569.     bye
  570. ;
  571.  
  572. : setup.main
  573.     0 ATPout !
  574.     open.mpp drop
  575.     open.atp drop
  576.     1 set.self.send drop
  577. ;
  578.  
  579. : setup.sender
  580.     setup.main
  581.     Sender ADD
  582.     Sender sendTask BUILD
  583.     Sender call selectwindow
  584.     sendTask mail.it
  585. ;
  586.  
  587. : setup.rcv
  588.     setup.main
  589.     Receiver ADD
  590.     Receiver rcvTask BUILD
  591.     Receiver call selectwindow
  592.     rcvTask get.it
  593. ;
  594.  
  595.